package com.elvin.platform.rabitMQ.consumeService;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
import org.springframework.stereotype.Component;


@Configuration
@Component
public class RabbitMqConfig implements RabbitListenerConfigurer {

  @Value("${spring.rabbitmq.host}")
  private String rabbitmqHost;

  @Value("${spring.rabbitmq.username}")
  private String rabbitmqUsername;

  @Value("${spring.rabbitmq.password}")
  private String rabbitmqPassword;

  @Value("${spring.rabbitmq.port}")
  private int rabbitmqPort;

  @Override
  public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
    rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
  }


  @Bean
  MessageHandlerMethodFactory messageHandlerMethodFactory(){
    DefaultMessageHandlerMethodFactory defaultMessageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
    defaultMessageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
    return defaultMessageHandlerMethodFactory;
  }

  @Bean
  public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
    return new MappingJackson2MessageConverter();
  }

  //新建队列
  @Bean
  Queue customerQueue(){
    return new Queue("recived-queue");
  }

  //交换机
  @Bean
  TopicExchange topicExchange() {
    return new TopicExchange("test_exchange");
  }

  //将customerQueue与topicExchange绑定起来：
  @Bean
  Binding myTopicBinding(){
    Binding binding = BindingBuilder.bind(customerQueue()).to(topicExchange()).with("D#.T#.P#.*");
    return binding;
  }


  @Bean
  public com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory() {
    com.rabbitmq.client.ConnectionFactory connectionFactory = new com.rabbitmq.client.ConnectionFactory();
    connectionFactory.setAutomaticRecoveryEnabled(true);
    connectionFactory.setUsername(rabbitmqUsername);
    connectionFactory.setPassword(rabbitmqPassword);
    connectionFactory.setHost(rabbitmqHost);
    connectionFactory.setPort(rabbitmqPort);
    connectionFactory.setVirtualHost("/");
    return connectionFactory;
  }

  @Bean("rabbitListenerContainerFactory")
  public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
    SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setPrefetchCount(2);
    factory.setConcurrentConsumers(3);
//        factory.setRecoveryInterval(recoveryInterval);
    configurer.configure(factory,  connectionFactory);
    return factory;
  }
    @Bean
     public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate template =new RabbitTemplate(factory);
         return template;
     }
}